/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.apps.epocket.stripes.action;

import able.stripes.JSP;
import com.apps.epocket.manager.MailManager;
import com.apps.epocket.manager.PengiklananManager;
import com.apps.epocket.model.MailBean;
import com.apps.epocket.model.Pengiklanan;
import com.apps.epocket.stripes.BaseActionBean;
import com.apps.epocket.stripes.StatusConstraints;
import com.google.inject.Inject;
import java.util.List;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;

/**
 *
 * @author fikri
 */
@UrlBinding("/approval")
public class ApprovalAdsActionBean extends BaseActionBean{
    
    @Inject PengiklananManager pengiklananManager;
    
    private Pengiklanan ads;
    
    private String idAds;
    
    private List<Pengiklanan> senaraiWaitingApproval;
    
    @Inject MailManager mailManager;
    
    @DefaultHandler
    public Resolution showForm() {
        senaraiWaitingApproval = pengiklananManager.senaraiPengiklananByStatus(StatusConstraints.PENDING_ADS);
        return new JSP("ads/ads_approval_list.jsp");
    }
    
    public Resolution approvalDetailForm() {
        ads = pengiklananManager.findById(Integer.valueOf(idAds));
        return new JSP("ads/ads_approval_detail.jsp");
    }
    
    public Resolution approve() {
        ads = pengiklananManager.findById(ads.getIdIklan());
        ads.setStatusPengiklanan(StatusConstraints.ACTIVE_ADS);
        pengiklananManager.save(ads);
        
        MailBean bean = new MailBean();
//        bean.setMailFrom("donotreply@pocket.my");
        bean.setMailTo(ads.getEmailPengiklan());        
        bean.setTitle("Notification from pocket.my");
        bean.setContent( getContent(ads.getNamaIklan(), StatusConstraints.ACTIVE_ADS) );
        mailManager.send(bean);
        
        return showForm();
    }
    
    public Resolution reject() {
        ads = pengiklananManager.findById(ads.getIdIklan());
        ads.setStatusPengiklanan(StatusConstraints.REJECT_ADS);
        pengiklananManager.save(ads);
        
        MailBean bean = new MailBean();
//        bean.setMailFrom("donotreply@pocket.my");
        bean.setMailTo(ads.getEmailPengiklan());        
        bean.setTitle("Notification from pocket.my");
        bean.setContent( getContent(ads.getNamaIklan(), StatusConstraints.REJECT_ADS) );
        mailManager.send(bean);
        
        return showForm();
    }
    
    
    String getContent(String nama, String result){
        StringBuilder sb = new StringBuilder();
        sb.append("Dear ").append(nama).append(", \n\n");
        
        sb.append("Your Ads has been ");
        switch (result) {
            case "AA" : 
                sb.append("approved. ");
                break;
            case "RA":
                sb.append("rejected.");
                break;
        }
        
        sb.append("If you have any enquiries, please do not hesitate to contact us at enquiry@pocket.my\n\n\n");
        sb.append("Thank You\n");
        
        sb.append("This email is auto generated.");
        return sb.toString();
    }   

    public List<Pengiklanan> getSenaraiWaitingApproval() {
        return senaraiWaitingApproval;
    }

    public void setSenaraiWaitingApproval(List<Pengiklanan> senaraiWaitingApproval) {
        this.senaraiWaitingApproval = senaraiWaitingApproval;
    }

    public Pengiklanan getAds() {
        return ads;
    }

    public void setAds(Pengiklanan ads) {
        this.ads = ads;
    }

    public String getIdAds() {
        return idAds;
    }

    public void setIdAds(String idAds) {
        this.idAds = idAds;
    }
}
